//------------------------------------------------------------------------------ 
//      Copyright (c) 2004, Be Nanashi-San 
// 
//      Permission is hereby granted, free of charge, to any person obtaining a 
//      copy of this software and associated documentation files (the "Software"), 
//      to deal in the Software without restriction, including without limitation 
//      the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//      and/or sell copies of the Software, and to permit persons to whom the 
//      Software is furnished to do so, subject to the following conditions: 
// 
//      The above copyright notice and this permission notice shall be included in 
//      all copies or substantial portions of the Software. 
// 
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//      FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//      DEALINGS IN THE SOFTWARE. 
// 
//      File Name:   JumpWindow.cpp
//      Author:      Be Nanashi-San 
//      Description: Jump to res GUI.
//------------------------------------------------------------------------------

#include <Button.h>
#include <TextControl.h>
#include <stdlib.h>

#include "2chPositive.h"
#include "JumpWindow.h"
#include "messages.h"
#include "Util.h"

static const unsigned int kOKBUTTON = 'kOKB';
static const unsigned int kCANSELBUTTON = 'kCLB';
static const unsigned int kJUMPTEXT = 'kJTT';

////////////////////////////////////////////////////////////////////////////////
// コンストラクタ
//
//
////////////////////////////////////////////////////////////////////////////////
JumpWindow::JumpWindow()
				: Dialog(IEtoBRect(300,200,221,82), "Jump")
{
	BView * contentView = new BView(this->Bounds(), "ContentView", 0, 0);
	contentView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
	this->AddChild(contentView);
	
	BTextControl* aTextControl = new BTextControl(IEtoBRect(15, 12, 184, 20), "input", "ジャンプする行番号：", "", new BMessage(kJUMPTEXT));
	aTextControl->SetDivider(123);
	contentView->AddChild(aTextControl);

	BButton* aButton = MakeButton(IEtoBRect(143,50,75,20), "OK", kOKBUTTON, contentView );
	this->SetDefaultButton(aButton);
	MakeButton(IEtoBRect(50,50,75,20), "Cansel", kCANSELBUTTON, contentView );
}
////////////////////////////////////////////////////////////////////////////////
// Show
// 入力効率化のため、既入力のテキストをSelectAllしておく。
//
////////////////////////////////////////////////////////////////////////////////
void
JumpWindow::Show()
{
	this->Lock();
	BTextControl *aTextControl = static_cast<BTextControl *>(this->FindView("input"));
	aTextControl->TextView()->SelectAll();
	aTextControl->MakeFocus(true);
	this->Unlock();

	BWindow::Show();
}

////////////////////////////////////////////////////////////////////////////////
// MessageReceived
//
//
////////////////////////////////////////////////////////////////////////////////
void
JumpWindow::MessageReceived(BMessage *msg)
{
	switch (msg->what){
		case kOKBUTTON:
		case kJUMPTEXT:{
			BTextControl *text = static_cast<BTextControl *>(this->FindView("input"));
			if(!text) break;
			int32 resno = atoi(text->Text());
			if (resno <= 0){
				text->TextView()->SelectAll();
				break;
			}
			
			this->Hide();
			BMessage jumpmessage(kMSG_JUMP);
			jumpmessage.AddInt32("jump", resno);
			be_app_messenger.SendMessage(&jumpmessage);
			break;
		}

		case kCANSELBUTTON:
			this->Hide();
			break;

		default:
			BWindow::MessageReceived (msg);
			break;
	}
}
